package com.cc.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.cc.models.User;
import com.cc.services.UserServiceI;
import com.cc.util.Pagination;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserServiceI userService;
	
	/**
	 * 用户列表
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/list",method={RequestMethod.POST,RequestMethod.GET})
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response){
		String pageNum = request.getParameter("page");
		int page = 1;
		if(pageNum!=null && !"".equals(pageNum))
			page = Integer.parseInt(pageNum);
		Map params = new HashMap();
		params.put("startRow", (page-1)*10);
		params.put("pageSize", 10);
		ModelAndView mav = new ModelAndView("user/list");
		List<User> list = userService.findUserWithPage(params);
		int countByParams = userService.countByParams(params);
		Pagination<User> p = new Pagination<User>();
		p.setRows(list);
		p.setPageNumber(page);
		p.setPageSize(10);
		p.setTotal(countByParams);
		request.setAttribute("userList", p);
		return mav;
	}
	
	/**
	 * 用户注册
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/register",method={RequestMethod.POST})
	public ModelAndView register(HttpServletRequest request,HttpServletResponse response){
		String userName = request.getParameter("userName");
		String userPassword = request.getParameter("userPassword");
		User user = new User();
		user.setUserName(userName);
		user.setUserPassword(userPassword);
		ModelAndView mav = new ModelAndView("login2");
		userService.addUser(user);
		request.getSession().setAttribute("USER", user);
		return mav;
	}
	
	/**
	 * 用户登录
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/login",method={RequestMethod.POST})
	public ModelAndView login(HttpServletRequest request,HttpServletResponse response){
		String userName = request.getParameter("userName");
		String userPassword = request.getParameter("userPassword");
		UsernamePasswordToken token = new UsernamePasswordToken(userName, userPassword);  
		Subject subject = SecurityUtils.getSubject();
		subject.login(token);
		ModelAndView mav = new ModelAndView("index");
		return mav;
	}
	
	/**
	 * 退出登录
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/logout",method=RequestMethod.POST)
	public ModelAndView logout(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mav = new ModelAndView("index");
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return mav;
	}
	
	// -------------------------GET一下是跳转方法--------------------------------------------
	
	/**
	 * 跳转到登录页面
	 * @return
	 */
	@RequestMapping(value="toLogin",method=RequestMethod.GET)
	public String toLogin(){
		return "login";
	}
	
	/**
	 * 跳转到注册页面
	 * @return
	 */
	@RequestMapping(value="toRegister",method=RequestMethod.GET)
	public String toRegister(){
		return "register";
	}
}
